﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Speedy
{
    public partial class frmRoute : XtraForm
    {
        DataTable Routlar;
        ISynchronizer sqlinstance;


        public frmRoute()
        {
            InitializeComponent();
            Routlar = new DataTable();
            Routlar.Columns.Add("Sıra", typeof(int));
            Routlar.Columns.Add("Müşteri Kodu", typeof(string));
            Routlar.Columns.Add("Müşteri Ünvanı", typeof(string));

            Routlar.Columns.Add("cari_id", typeof(int));
            Routlar.Columns.Add("adres_id", typeof(int));

            string defaultval = "";
            if (Config.GetConfig.GetValue(CParams.Db).IndexOf("Mikro") > -1)
                defaultval = "Mikro V12";
            else
                defaultval = "WorkGroup";

            if (Config.GetConfig.GetValue(CParams.ProgramType, defaultval) == "WorkGroup")
                sqlinstance = new syncWorkGrup();
            else
                sqlinstance = new syncMikroV12();
        }

        private void frmRoute_Load(object sender, EventArgs e)
        {
            dbMerchant pls = new dbMerchant();
            pls.name.Alias = "NAME";
            pls.id.Alias = "ID";
            DataTable dt = pls.AllData();
            if (dt.Rows.Count > 0)
            {
                cmbPlasiyer.DisplayMember = "NAME";
                cmbPlasiyer.ValueMember = "ID";
                cmbPlasiyer.DataSource = dt;
                cmbPlasiyer.SelectedIndex = 0;
            }

            for (int i = 1; i < 6; i++)
                cmbHafta.Items.Add(i.ToString());

            cmbGun.Items.Add("Pazartesi");
            cmbGun.Items.Add("Salı");
            cmbGun.Items.Add("Çarşamba");
            cmbGun.Items.Add("Perşembe");
            cmbGun.Items.Add("Cuma");
            cmbGun.Items.Add("Cumartesi");
            cmbGun.Items.Add("Pazar");
            cmbGun.Items.Add("Tüm Günler");

            gridControl2.DataSource = DataLayer.GetLayer.FillData(sqlinstance.CariListeSql());
            gridView2.Columns["cari_RECno"].Visible = false;
            gridView2.Columns["adres"].Visible = false;
            gridView2.BestFitColumns();

            gridControl1.DataSource = Routlar;
            gridView1.Columns["cari_id"].Visible = false;
            gridView1.Columns["adres_id"].Visible = false;
        }

        void FillRoute()
        {
            Routlar.Rows.Clear();

            SqlParameter week = new SqlParameter("@week", SqlDbType.Int);
            week.Value = cmbHafta.SelectedIndex + 1;
            SqlParameter day = new SqlParameter("@day", SqlDbType.Int);
            day.Value = cmbGun.SelectedIndex + 1;
            SqlParameter pls = new SqlParameter("@pls", SqlDbType.Int);
            pls.Value = Convert.ToInt32(cmbPlasiyer.SelectedValue);

            DataTable dt = DataLayer.GetLayer.FillData(sqlinstance.RouteListeSql(), week, day, pls);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Routlar.Rows.Add(dt.Rows[i]["VISIT_NO"], dt.Rows[i]["cari_kod"], dt.Rows[i]["cari_unvan1"], dt.Rows[i]["CUSTOMER_ID"], dt.Rows[i]["ADDRESS_ID"]);
            }
            gridView1.BestFitColumns();
        }

        bool route_exists(string cari_id, string adres_id)
        {
            for (int i = 0; i < Routlar.Rows.Count; i++)
                if (Routlar.Rows[i]["cari_id"].ToString() == cari_id && Routlar.Rows[i]["adres_id"].ToString() == adres_id)
                    return true;
            return false;
        }

        private void gridView2_DoubleClick(object sender, EventArgs e)
        {
            GridEkle();
        }

        private void GridEkle()
        {
            if (cmbGun.SelectedIndex > -1 && cmbHafta.SelectedIndex > -1)
            {
                if (gridView2.SelectedRowsCount > 0)
                {
                    string carikod = string.Empty;
                    string unvani = string.Empty;
                    int adres_id;
                    int cari_id;
                    int adres_no;

                    foreach (int i in gridView2.GetSelectedRows())
                    {
                        carikod = gridView2.GetRowCellDisplayText(i, gridView2.Columns["Müşteri Kodu"]);
                        unvani = gridView2.GetRowCellDisplayText(i, gridView2.Columns["1.Ünvanı"]);
                        adres_id = int.Parse(gridView2.GetRowCellDisplayText(i, gridView2.Columns["Adres No"]));
                        cari_id = int.Parse(gridView2.GetRowCellDisplayText(i, gridView2.Columns["cari_RECno"]));
                        adres_no = int.Parse(gridView2.GetRowCellDisplayText(i, gridView2.Columns["adres"]));
                        if (!route_exists(cari_id.ToString(), adres_id.ToString()))
                            Routlar.Rows.Add(Routlar.Rows.Count + 1, carikod, unvani, cari_id, adres_id);
                    }
                }
                gridView1.BestFitColumns();
            }
            else
                MessageBox.Show("Öncelikli Olarak Gün ve Haftayı Seçmelisiniz !");
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void cmbGun_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbGun.SelectedIndex > -1 && cmbHafta.SelectedIndex > -1)
                FillRoute();
        }

        private void cmbHafta_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbGun.SelectedIndex > -1 && cmbHafta.SelectedIndex > -1)
                FillRoute();
        }

        private void cmbPlasiyer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbGun.SelectedIndex > -1 && cmbHafta.SelectedIndex > -1)
                FillRoute();
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (cmbGun.SelectedIndex == 7)
            {
                for (int i = 1; i < 8; i++)
                {
                    RouteKaydet(i, cmbHafta.SelectedIndex + 1, Convert.ToInt32(cmbPlasiyer.SelectedValue));
                }
            }
            else
                RouteKaydet(cmbGun.SelectedIndex + 1, cmbHafta.SelectedIndex + 1, Convert.ToInt32(cmbPlasiyer.SelectedValue));

            MessageBox.Show("Kayıt Tamamlandı !");
        }

        private void RouteKaydet(int gun, int hafta, int plasiyer_id)
        {
            if (cmbGun.SelectedIndex > -1 && cmbHafta.SelectedIndex > -1)
            {
                dbRoute r = new dbRoute();
                int sira;
                int cari_id;
                int adres_id;

                //kayıtlar siliniyor ilk önce
                r.day_no.SetValue(gun);
                r.merchant_id.SetValue(plasiyer_id);
                r.week_no.SetValue(hafta);
                r.Remove();

                for (int i = 0; i < Routlar.Rows.Count; i++)
                {
                    sira = Convert.ToInt32(Routlar.Rows[i]["Sıra"]);
                    cari_id = Convert.ToInt32(Routlar.Rows[i]["cari_id"]);
                    adres_id = Convert.ToInt32(Routlar.Rows[i]["adres_id"]);
                    r.address_id.SetValue(adres_id);
                    r.customer_id.SetValue(cari_id);
                    r.day_no.SetValue(gun);
                    r.merchant_id.SetValue(plasiyer_id);
                    r.visit_no.SetValue(sira);
                    r.week_no.SetValue(hafta);
                    r.Save();
                }

            }
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (cmbGun.SelectedIndex == 7)
            {
                for (int i = 1; i < 8; i++)
                {
                    RouteSil(i, cmbHafta.SelectedIndex + 1, Convert.ToInt32(cmbPlasiyer.SelectedValue));
                }
            }
            else
                RouteSil(cmbGun.SelectedIndex + 1, cmbHafta.SelectedIndex + 1, Convert.ToInt32(cmbPlasiyer.SelectedValue));

            MessageBox.Show("Kayıt Silindi !");
            FillRoute();
        }

        private void RouteSil(int gun, int hafta, int plasiyer_id)
        {
            if (cmbHafta.SelectedIndex > -1 && cmbGun.SelectedIndex > -1)
            {
                dbRoute rt = new dbRoute();
                rt.week_no.SetValue(hafta);
                rt.day_no.SetValue(gun);
                rt.merchant_id.SetValue(plasiyer_id);
                rt.Remove();
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int[] rows = gridView1.GetSelectedRows();
                Queue<DataRow> q = new Queue<DataRow>();

                if (rows.Length > 0)
                    foreach (int item in rows)
                        q.Enqueue(((System.Data.DataRowView)(gridView1.GetRow(item))).Row);

                while (q.Count > 0)
                    Routlar.Rows.Remove(q.Dequeue());
            }
        }

        private void gridView2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                GridEkle();
        }
    }
}
